package com.wzx.ptplatform.service.impl;

import com.wzx.ptplatform.dao.UserDao;
import com.wzx.ptplatform.domain.User;
import com.wzx.ptplatform.service.UserService;
import com.wzx.ptplatform.utils.PtplatformException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 紫煦桉
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public User login(User user) throws PtplatformException {
        User userMessage = userDao.findByUserName(user.getUserName());
        if (userMessage != null && userMessage.getPassword().equals(user.getPassword())) {
            return userMessage;
        }
        return null;
    }

    @Override
    public boolean resetPassword(User user) throws PtplatformException {
        try {
            User userMessage = userDao.findById(user.getId()).orElse(null);
            assert userMessage != null;
            userMessage.setPassword(user.getPassword());
            userDao.saveAndFlush(userMessage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public User findById(long id) throws PtplatformException {
        return userDao.findById(id).orElse(null);
    }
}
